package com.pr.makfilm.user.pages.room.create;

import com.pr.makfilm.core.ajax.ConfirmationCallListener;
import com.pr.makfilm.core.cmp.authorization.UserAuthorities;
import com.pr.makfilm.core.cmp.web.BootstrapTextFieldPanel;
import com.pr.makfilm.domain.Room;
import com.pr.makfilm.service.IRoomService;
import com.pr.makfilm.user.api.UserApiMappings;
import com.pr.makfilm.user.navigation.NavigateToConstructRoomPage;
import com.pr.makfilm.user.navigation.NavigateToRoomListPage;
import com.pr.makfilm.user.pages.AbstractUserPage;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.attributes.AjaxRequestAttributes;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.authroles.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.validation.validator.EmailAddressValidator;
import org.apache.wicket.validation.validator.StringValidator;
import org.wicketstuff.annotation.mount.MountPath;

/**
 * Created with IntelliJ IDEA.
 * User: Pawel
 * Date: 27.09.13
 * Time: 01:16
 * To change this template use File | Settings | File Templates.
 */
@MountPath(UserApiMappings.CREATE_ROOM_PAGE)
@AuthorizeInstantiation(UserAuthorities.USER)
public class CreateRoomPage extends AbstractUserPage
{
   @SpringBean
   private IRoomService roomService;

   private Room room = new Room();

   @Override
   protected void onInitialize()
   {
      super.onInitialize();
      Form form = new Form("form")
      {
         @Override
         protected void onSubmit()
         {
            room = roomService.create(room);
            new NavigateToConstructRoomPage(room).navigate();
         }
      };

      BootstrapTextFieldPanel name = new BootstrapTextFieldPanel<String>("name", new PropertyModel<String>(this, "room." + Room.FIELD_NAME));
      name.setValidation(true);
      name.addValidator(StringValidator.maximumLength(Room.MAX_LENGTH_NAME));
      form.add(name);
      BootstrapTextFieldPanel taxId = new BootstrapTextFieldPanel<String>("tax.id", new PropertyModel<String>(this, "room." + Room.FIELD_TAX_ID));
      taxId.setValidation(false);
      taxId.addValidator(StringValidator.exactLength(Room.MAX_LENGTH_TAX_ID));
      form.add(taxId);
      BootstrapTextFieldPanel capacity = new BootstrapTextFieldPanel<Integer>("capacity", new PropertyModel<Integer>(this, "room." + Room.FIELD_CAPACITY));
      capacity.setValidation(true);
      form.add(capacity);
      BootstrapTextFieldPanel price = new BootstrapTextFieldPanel<Double>("price", new PropertyModel<Double>(this, "room." + Room.FIELD_LEASE_PRICE));
      price.setValidation(false);
      form.add(price);

      BootstrapTextFieldPanel street = new BootstrapTextFieldPanel<String>("street", new PropertyModel<String>(this, "room." + Room.FIELD_STREET));
      street.setValidation(true);
      street.addValidator(StringValidator.maximumLength(Room.MAX_LENGTH_STREET));
      form.add(street);
      BootstrapTextFieldPanel zip = new BootstrapTextFieldPanel<String>("zip", new PropertyModel<String>(this, "room." + Room.FIELD_ZIP));
      zip.setValidation(true);
      zip.addValidator(StringValidator.maximumLength(Room.MAX_LENGTH_ZIP));
      form.add(zip);
      BootstrapTextFieldPanel city = new BootstrapTextFieldPanel<String>("city", new PropertyModel<String>(this, "room." + Room.FIELD_CITY));
      city.setValidation(true);
      city.addValidator(StringValidator.maximumLength(Room.MAX_LENGTH_CITY));
      form.add(city);

      BootstrapTextFieldPanel firstName = new BootstrapTextFieldPanel<String>("first.name", new PropertyModel<String>(this, "room." + Room.FIELD_CONTACT_FIRST_NAME));
      firstName.setValidation(false);
      firstName.addValidator(StringValidator.maximumLength(Room.MAX_LENGTH_FIRST_NAME));
      form.add(firstName);
      BootstrapTextFieldPanel lastName= new BootstrapTextFieldPanel<String>("last.name", new PropertyModel<String>(this, "room." + Room.FIELD_CONTACT_LAST_NAME));
      lastName.setValidation(false);
      lastName.addValidator(StringValidator.maximumLength(Room.MAX_LENGTH_LAST_NAME));
      form.add(lastName);
      BootstrapTextFieldPanel phone = new BootstrapTextFieldPanel<String>("phone", new PropertyModel<String>(this, "room." + Room.FIELD_PHONE));
      phone.setValidation(false);
      phone.addValidator(StringValidator.maximumLength(Room.MAX_LENGTH_PHONE));
      form.add(phone);
      BootstrapTextFieldPanel mobile = new BootstrapTextFieldPanel<String>("mobile", new PropertyModel<String>(this, "room." + Room.FIELD_MOBILE));
      mobile.setValidation(false);
      mobile.addValidator(StringValidator.maximumLength(Room.MAX_LENGTH_PHONE));
      form.add(mobile);
      BootstrapTextFieldPanel<String> email = new BootstrapTextFieldPanel<String>("email", new PropertyModel<String>(this, "room." + Room.FIELD_EMAIL));
      email.addValidator(EmailAddressValidator.getInstance());
      email.setValidation(false);
      email.addValidator(StringValidator.maximumLength(Room.MAX_LENGTH_EMAIL));
      form.add(email);

      BootstrapTextFieldPanel columns = new BootstrapTextFieldPanel<Integer>("columns", new PropertyModel<Integer>(this, "room." + Room.FIELD_COLUMN_COUNT));
      columns.setValidation(true);
      form.add(columns);
      BootstrapTextFieldPanel rows = new BootstrapTextFieldPanel<Integer>("rows", new PropertyModel<Integer>(this, "room." + Room.FIELD_ROW_COUNT));
      rows.setValidation(true);
      form.add(rows);

      form.add(new AjaxLink<Void>("cancel") {
         @Override
         public void onClick(AjaxRequestTarget target)
         {
            new NavigateToRoomListPage().navigate();
         }

         @Override
         protected void updateAjaxAttributes(AjaxRequestAttributes attributes)
         {
            super.updateAjaxAttributes(attributes);
            attributes.getAjaxCallListeners().add(new ConfirmationCallListener(getString("create.room.clean.confirm")));
         }
      });
      add(form);
   }
}
